package com.lineage.data.item_etcitem.teleport;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.lock.CharBookReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_Paralysis;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1BookMark;
import java.util.ArrayList;

public class Move_Reel extends ItemExecutor {
  public static ItemExecutor get() {
    return new Move_Reel();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    Short mapID = Short.valueOf((short)data[0]);
    int mapX = data[1];
    int mapY = data[2];
    boolean isTeleport = pc.getMap().isTeleportable();
    if (!isTeleport) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(647));
      pc.sendPackets((ServerBasePacket)new S_Paralysis(7, false));
    } else {
      boolean flag = false;
      ArrayList<L1BookMark> bookList = CharBookReading.get().getBookMarks(pc);
      if (bookList != null) {
        for (L1BookMark book : bookList) {
          if (book.getMapId() == mapID.shortValue() && book.getLocX() == mapX && 
            book.getLocY() == mapY)
            flag = true; 
        } 
      } else {
        L1Teleport.randomTeleport(pc, true);
      } 
      if (flag) {
        pc.getInventory().removeItem(item, 1L);
        L1Teleport.teleport(pc, mapX, mapY, mapID.shortValue(), 5, true);
      } else {
        pc.getInventory().removeItem(item, 1L);
        L1Teleport.randomTeleport(pc, true);
      } 
    } 
  }
}
